<template>
	<view class="page">
		<view class="page-bg">
			<image style="width: 100%; height: 597rpx;" src="https://test.uaima.cn/public/static/img/bg.png" mode=""></image>
		</view>
		<view class="page-navbar" style="position: sticky;top: 0rpx;z-index: 11;"
			:style="{paddingTop: `${statusBarHeight + 10}px`, width: `calc(100% - ${MenuButtonBoundingClientRectWidth + 30}px)`}">
			<view style="font-size: 34rpx;color: #181818;line-height: 40rpx;">
				购物车
			</view>
			<view style="font-weight: 400;font-size: 30rpx;color: #67B609;line-height: 35rpx;">
				管理
			</view>
		</view>
		<view class="page-cart">
			<view class="page-cart-box">
				<view class="empty" v-if="list.length === 0">
					<image class="emptyImg" src="https://test.uaima.cn/public/static/img/mine/emptySearch.png"></image>
					<view>暂无内容~</view>
				</view>
				<view class="page-cart-item" v-for="item,index in list" :key="index">
					<view style="display: flex; align-items: center;justify-content: center; margin-right: 21rpx;"
						@click="onSelect(item)">
						<image v-if="item.active" style="width: 40rpx; height: 40rpx;" src="https://test.uaima.cn/public/static/icon/circle_select.png"
							mode="">
						</image>
						<image v-else style="width: 40rpx; height: 40rpx;" src="https://test.uaima.cn/public/static/icon/circle_no_select.png" mode="">
						</image>
					</view>
					<view class="page-cart-item-info w-full">
						<u--image :showLoading="true" radius='8rpx' :src="item.productInfo.image" width="180rpx"
							height="180rpx"></u--image>
						<view class="page-cart-item-info-right w-full ml-20">
							<view class="page-cart-item-info-right-title">
								{{ item.productInfo.storeName }}
							</view>
							<view class="page-cart-item-info-right-doc">
								{{ item.productInfo.storeInfo }}
							</view>
							<view class="page-cart-item-info-right-price">
								<view>
									<text style="font-size: 24rpx;">￥</text>
									<text> {{ item.productInfo.price }} </text>
								</view>
								<u-number-box v-model="item.cartNum" @change='(val)=>changeCartNum(val,item)'>
									<view slot="minus" class="minus">
										-
									</view>
									<view slot="plus" class="plus">
										+
									</view>
								</u-number-box>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="page-bottom">
			<view class="page-bottom-select">
				<view style="display: flex; align-items: center;justify-content: center; margin-right: 32rpx;"
					@click="selectAllFun">
					<image v-if="!selectAllIds" style="width: 40rpx; height: 40rpx;" src="https://test.uaima.cn/public/static/icon/circle_no_select.png"
						mode=""></image>
					<image v-else style="width: 40rpx; height: 40rpx;" src="https://test.uaima.cn/public/static/icon/circle_select.png" mode=""></image>
				</view>
				全选
			</view>
			<view class="page-bottom-price" style="font-size: 28rpx;color: #222222;line-height: 33rpx;">
				合计
				<text style="font-weight: bold;font-size: 34rpx;color: #ED7B42;line-height: 60rpx; margin-left: 20rpx;">
					{{ totalPrice }}</text>
			</view>
			<view class="page-bottom-btn" @click="settleAnAccount">
				结算
			</view>
		</view>
	</view>
</template>

<script>
	import msg from '../../utils/msg'
	export default {
		data() {
			return {
				statusBarHeight: 0,
				MenuButtonBoundingClientRectWidth: 0,
				list: [],
				selectId: [],
				selectAllIds: false,
				totalPrice: '0.00'
			}
		},
		onLoad() {
			this.getHeight()
		},
		onShow() {
			this.cartList()
		},
		methods: {
			settleAnAccount() {
				if (this.selectId.length === 0) return msg.toast("请选择商品")
				uni.navigateTo({
					url: "/pages/product/confirmOrder" + `?type=1&ids=${this.selectId.join()}`
				})
			},
			selectAllFun() {
				// 取消全选
				if (this.selectAllIds) {
					this.list.forEach(item => item.active = false)
				} else {
					this.list.forEach(item => item.active = true)
				}

				this.isSelectAll()
			},
			changeCartNum(val, item) {
				this.$iApi.cartChangeNum({
					id: item.id,
					number: val.value
				}).catch(err => {
					this.cartList()
				})
			},
			isSelectAll() {
				let n = 0;
				let price = 0
				let ids = []
				this.list.forEach(item => {
					if (item.active) {
						price += +item.productInfo.price * item.cartNum
						ids.push(item.id)
						n++
					};
				})
				if (n === this.list.length) this.selectAllIds = true;
				else {
					this.selectAllIds = false;
				}

				this.selectId = ids;
				this.totalPrice = price.toFixed(2)
			},
			cartList() {
				this.$iApi.cartList({
					ids: ''
				}).then(res => {
					this.list = res.data.valid.map(item => {
						return {
							...item,
							active: false,
						}
					});
				})
			},
			// 获取微信右上角胶囊高度
			getHeight() {
				const systemInfo = uni.getSystemInfoSync();
				this.statusBarHeight = systemInfo.statusBarHeight;
				const MenuButtonBoundingClientRect = wx.getMenuButtonBoundingClientRect()
				this.MenuButtonBoundingClientRectWidth = MenuButtonBoundingClientRect.width
			},
			onSelect(item) {
				item.active = !item.active
				this.isSelectAll()
			}
		},

	}
</script>
<style>
	page {
		background-color: #F5F5F5;
	}
</style>
<style lang="scss" scoped>
	.page {
		&-bg {
			position: fixed;
			top: 0;
			width: 100%;
			height: 597rpx;
		}

		&-navbar {
			position: relative;
			z-index: 1;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-left: 30rpx;
		}

		&-cart {
			position: relative;
			z-index: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			overflow: auto;
			padding: 30rpx;
			padding-bottom: 134rpx;

			&-box {
				width: 100%;
				overflow: hidden;
			}

			&-item {
				height: calc(240rpx - 44rpx);
				background: #FFFFFF;
				border-radius: 16rpx;
				display: flex;
				padding: 30rpx 20rpx 20rpx 22rpx;
				margin-bottom: 24rpx;

				&-info {
					display: flex;
					align-items: center;
					flex-direction: row;

					&-right {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						height: 100%;

						&-title {
							font-weight: 400;
							font-size: 28rpx;
							color: #333333;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 2;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: normal;
							width: 100%;
						}

						&-doc {
							width: max-content;
							background: rgba(237, 123, 66, 0.06);
							border-radius: 4rpx 4rpx 4rpx 4rpx;
							border: 1rpx solid rgba(237, 123, 66, 0.3);
							padding: 6rpx 10rpx;
							font-weight: 400;
							font-size: 24rpx;
							color: #ED7B42;
							line-height: 30rpx;
						}

						&-price {
							width: 100%;
							display: flex;
							align-items: center;
							justify-content: space-between;
							font-weight: 500;
							font-size: 32rpx;
							color: #ED7B42;

							.minus {
								width: 50rpx;
								height: 50rpx;
								border-radius: 50%;
								border: 1px solid #ccc;
								display: flex;
								align-items: center;
								justify-content: center;
								color: #000;
								font-size: 40rpx;
							}

							.plus {
								width: 50rpx;
								height: 50rpx;
								border-radius: 50%;
								border: 1px solid #ccc;
								display: flex;
								align-items: center;
								justify-content: center;
								color: #FFF;
								font-size: 40rpx;
								background: #67B609;
							}

							::v-deep .u-number-box {
								background: #F7FBF3 !important;
								border-radius: 200rpx;

								.u-number-box__input {
									background: #F7FBF3 !important;
								}
							}
						}
					}
				}
			}
		}

		&-bottom {
			position: fixed;
			bottom: 0;
			z-index: 2;
			width: 100vw;
			height: 110rpx;
			background: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: space-between;

			&-select {
				margin-left: 31rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 32rpx;
				color: #222222;
			}

			&-price {
				display: flex;
				align-items: center;
			}

			&-btn {
				margin-right: 30rpx;
				width: 200rpx;
				height: 88rpx;
				background: #67B609;
				border-radius: 44rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-weight: bold;
				font-size: 32rpx;
				color: #FFFFFF;
				line-height: 38rpx;
			}

		}
	}
</style>